#include "Scene_PCH.h"
#include "bMesh.h"

/*--------------------------------------------------------------------------*/

bMesh::bMesh(btDiscreteDynamicsWorld* world, const bInitData& initdata) :
	bObject(world)
{
	m_collisionShape = new btSphereShape(1);
	AddRigidBody(m_collisionShape, initdata);

	m_material = NULL;
}

/*--------------------------------------------------------------------------*/

bMesh::~bMesh()
{
	RemoveRigidBody();
}

/*--------------------------------------------------------------------------*/

const RMaterial* matt = NULL;
void bMesh::PreRendering( uint arg )
{
	if (m_vertexarray == NULL)
	{
		m_vertexarray = g_Renderer->GetRendererStorageMgr()->GetVertexArray("sphere_0");
	}

	if (m_material == NULL)
	{
		if (matt == NULL)
		{
			MaterialDesc md;
			md.m_flags = EMaterialDesc::ColorAsTexture0 | EMaterialDesc::OutNormal | EMaterialDesc::UseBumpmapping| EMaterialDesc::EnableInstancing;
			md.m_Bumpmapping_normal0 = g_Renderer->GetRendererStorageMgr()->GetTexture("normal");
			md.m_Bumpmapping_TextureMultiplier = 1.0f;
			md.m_Bumpmapping_Strength = 0.1f;
			md.m_texture0 = g_Renderer->GetRendererStorageMgr()->GetTexture("normal");
			
			m_material = g_Renderer->GetMaterialMgr()->GetMaterialFromMaster(md);
			matt = m_material;
		}
		else
		{
			m_material = matt;
		}


	}

	switch (arg)
	{
	case 0:
		{
			g_Renderer->GetQueueMgr()->AddTask(this, m_material, m_vertexarray, ETopology::Triangles, m_vertexarray->GetNumIndices(), 0);
			break;
		}
	}
}

/*--------------------------------------------------------------------------*/

void bMesh::WriteModelMatrix( float* modelmatrix ) const
{
	btTransform transform;
	m_rigidBody->getMotionState()->getWorldTransform(transform);
	transform.getOpenGLMatrix(modelmatrix);
}

/*--------------------------------------------------------------------------*/